<?php
/**
 * bs多规格sku商城系统
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2025/3/15
 * 描述:
 */

namespace app\service;

use app\model\auto\CreditLog;
use app\model\auto\PointLog;
use app\model\user\User;
use think\facade\Db;

class CreditService
{
    /**
     * 余额交易
     * @param $user_id
     * @param $num float 数量，有正负
     * @param $args array ['order_sn', 'remark']
     * @return bool
     */
    public static function credit_action($user_id, $num, $args): bool
    {
        if (!$user_id) {
            return false;
        }
        if (!$num) {
            return false;
        }
        if (!$args) {
            return false;
        }
        $before_num = (new User())->get_value(['user_id' => $user_id], 'credit');
        if ($num < 0) {
            $after_num = $before_num - abs($num);
        } else {
            $after_num = $before_num + $num;
        }

        $data = [
            'user_id' => $user_id,
            'before_num' => $before_num,
            'num' => $num,
            'after_num' => $after_num,
            'remark' => $args['remark'] ?? '',
            'order_sn' => $args['order_sn'] ?? '',
        ];
        Db::startTrans();
        try {
            $res = (new User())->data_update(['user_id' => $user_id], ['credit' => $after_num]);
            if (!$res) {
                throw new \Exception('更新余额失败');
            }
            $log = (new CreditLog())->data_add($data);
            if (!$log) {
                throw new \Exception('添加记录失败');
            }
        } catch (\Exception $e) {
            Db::rollback();
            bs_log(['err' => $e->getMessage(), 'data' => $data], 'error', 'credit_log.log');
            return false;
        }
        Db::commit();
        return true;
    }

    /**
     * 积分交易
     * @param $user_id
     * @param $num float 数量，有正负
     * @param $args array ['order_sn', 'remark']
     * @return bool
     */
    public static function point_action($user_id, $num, $args): bool
    {
        if (!$user_id) {
            return false;
        }
        if (!$num) {
            return false;
        }
        if (!$args) {
            return false;
        }
        $before_num = (new User())->get_value(['user_id' => $user_id], 'point');
        if ($num < 0) {
            $after_num = $before_num - abs($num);
        } else {
            $after_num = $before_num + $num;
        }
        $data = [
            'user_id' => $user_id,
            'before_num' => $before_num,
            'num' => $num,
            'after_num' => $after_num,
            'remark' => $args['remark'] ?? '',
            'order_sn' => $args['order_sn'] ?? '',
        ];
        Db::startTrans();
        try {
            $res = (new User())->data_update(['user_id' => $user_id], ['point' => $after_num]);
            if (!$res) {
                throw new \Exception('更新积分失败');
            }
            $log = (new PointLog())->data_add($data);
            if (!$log) {
                throw new \Exception('添加记录失败');
            }
        } catch (\Exception $e) {
            Db::rollback();
            bs_log($e->getMessage(), 'error', 'point_log.log');
            return false;
        }
        Db::commit();
        return true;
    }
}
